module neo.tetris.rpc.messageImpl {
	/**
	 *
	 * @author 
	 *
	 */
	export class BattleMessageImpl implements neo.tetris.rpc.message.BattleMessage {
		public constructor() {
		}
		
		/**
		 * 战斗开始
		 *
		 * @param startTime 
		 */
        public battleStart(startTime: number): void {
            neo.tetris.model.appInstance.battle.startTime = startTime;
        }


		/**
		 * 开始匹配
		 *
		 */
        public battleMatching(): void {
            neo.tetris.model.appInstance.battle = new neo.tetris.model.BattleModel();
        }


		/**
		 * 战斗结束
		 *
		 * @param winnerId 
		 */
        public battleEnd(winnerId: number): void {
            var battle:neo.tetris.model.BattleModel = neo.tetris.model.appInstance.battle;
            battle.winner = battle.players[winnerId];
        }


		/**
		 * 玩家加入
		 *
		 * @param playerIndex 
		 * @param playerUuid 
		 * @param playerName 
		 */
        public playerSet(playerIndex: number,playerUuid: string,playerName: string,playerHeadIcon: string): void {
            var player: neo.tetris.model.BattlePlayerModel = new neo.tetris.model.BattlePlayerModel(neo.tetris.model.appInstance.battle);
            player.id = playerIndex;
            player.userUUid = playerUuid;
            player.name = playerName;
            player.headIcon = playerHeadIcon;
            neo.tetris.model.appInstance.battle.players[playerIndex] = player;
            neo.tetris.model.appInstance.battle.dispatchEventWith(neo.typescript.model.Bean.PROPERTY_CHANGE,false,neo.tetris.model.BattleModel.PLAYERS);
        }
        
        /**
         * 同步状态
         *
         * @param playerIndex 
         * @param state 
         */
        public stateSynchronize(playerIndex: number,state: string): void {
            var battle: neo.tetris.model.BattleModel = neo.tetris.model.appInstance.battle;
            var player:neo.tetris.model.BattlePlayerModel = battle.players[playerIndex];
            if (player.userUUid == neo.tetris.model.appInstance.logined.uuid) {
                // do nothing
            } else {
                console.log("update other player state " + state);
                var sp:string[] = state.split("|");
                var shapes: neo.tetris.model.ShapeModel[] = [];
                for(var i: number = 0;i < sp.length; i+=2) {
                    shapes[sp[i]] = neo.tetris.model.TetrisModel.getShape(parseInt(sp[i + 1]));
                }
                player.tetris.shapes = shapes;
            }
        }

        /**
		 * 增加难度
		 *
		 * @param playerIndex 
		 */
		public addDifficult(playerIndex:number):void {
            for(var i: number = 0;i < neo.tetris.model.appInstance.battle.players.length; i++) {
                var player:neo.tetris.model.BattlePlayerModel = neo.tetris.model.appInstance.battle.players[i];
                if (player.id == playerIndex) {
                    player.addDifficultTime = egret.getTimer();
                } else {
                    for(var r = neo.tetris.model.TetrisModel.row - 1;r > -1;r--) {
                        for(var c: number = 0;c < neo.tetris.model.TetrisModel.column;++c) {
                            player.tetris.shapes[r * neo.tetris.model.TetrisModel.column + c] = player.tetris.shapeAt(c,r - 1);
                        }
                    }
                    r = 0;
                    var empty: number = Math.floor(Math.random() * neo.tetris.model.TetrisModel.column);
                    for(var c: number = 0;c < neo.tetris.model.TetrisModel.column;++c) {
                        player.tetris.shapes[r * neo.tetris.model.TetrisModel.column + c]
                            = empty == c ? null : neo.tetris.model.TetrisModel.getShape(1);
                    }
                }
            }
            
		}

	}
}
